package com.eos.control.action.emp;

import java.util.Date;

import com.eos.business.service.SysUserBusinessService;
import com.eos.common.tool.DataCacheUtil;
import com.eos.common.tool.ObjectUtils;
import com.eos.control.action.base.AbstractBaseAction;
import com.eos.orm.mybatis.model.SysUser;
import com.hf.utils.MD5Util;

public class SaveEmpAction extends AbstractBaseAction {
	private static final long serialVersionUID = -5920774635731513867L;
	private String userName;
	private String userEmail;
	private String password;
	private String telephone;
	private String userMobile;
	private String userDesc;
	private Integer roleId;

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public void setUserMobile(String userMobile) {
		this.userMobile = userMobile;
	}

	public void setUserDesc(String userDesc) {
		this.userDesc = userDesc;
	}

	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	@Override
	public String execute() {
		try {
			SysUser su = DataCacheUtil.getSysUser(userEmail);
			if(su != null){
				statusCode = "300";
				message = "你输入的邮箱已被占用, 请换一个再试!";
			}else{
				SysUser user = new SysUser();
				user.setUserId(ObjectUtils.generateRandomString(16));
				user.setDealFlag("0");
				user.setPassWord(new MD5Util().encrypt(password));
				user.setStatus("0");
				user.setUserDesc(userDesc);
				user.setUserEmail(userEmail);
				user.setUserMobile(userMobile);
				user.setUserName(userName);
				user.setUserTelephone(telephone);
				Date date = new Date(System.currentTimeMillis());
				user.setInsertTime(ObjectUtils.formatDate(date, null));
				user.setModifyTime(ObjectUtils.formatDate(date, null));
				user.setRoleId(roleId == null ? 0 : roleId);
				SysUserBusinessService sbs = new SysUserBusinessService();
				String flag = sbs.save(user); 
				if("SUCCESS".equals(flag)){
					statusCode = "200";
					message = "新增用户成功!";
					DataCacheUtil.clearSysUserMap();
				}else{
					statusCode = "300";
					message = "系统繁忙, 新增用户失败!";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			statusCode = "300";
			message = "系统异常,新增用户失败!";
		}
		return SUCCESS;
	}
}
